<?php

namespace App\Common\Controllers;

use App\Common\Facades\ComEmailCodeFacade;
use App\Common\Facades\ComSmsFacade;
use App\Common\Facades\UploadFileFacade;
use App\Common\Models\ComEmailCodeModel;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class CommonController extends BaseController
{

    /**
     * 获取验证码 phone 手机 type 验证类型 1登录验证 2注册验证 3忘记密码
     */
    public function getSMSCode(Request $request){
        $this->validate($request, [
            'phone' => 'required'
        ], [
            'phone.required' => '手机不能为空'
        ]);
        $params = $request->only(['phone', 'is_send', 'country_code', 'app_code']);
        $phone=$params['phone'];
        $smsVerify = ComSmsFacade::validataPhoneMinute($phone,0);
        if ($smsVerify['code'] == -1) {
            return $this->jsonResponse('', '',10020);
        }
        $ret=ComSmsFacade::sendSMSInfo($params);
        if($ret['code'] == 0){
            return $this->jsonResponse('ok', $ret['data']);
        }else{
            return $this->jsonResponse($ret['message'], $ret['data'],$ret['code']);
        }
    }

    /**
     * 获取邮箱验证码
     */
    public function getEmailCode(Request $request){
        $email=$request->input('email','');
        $isSend=$request->input('is_send',1);
        $language=$request->input('language','zh-cn');
        $footer=$request->input('footer','');
        $this->validate($request, [
            'email' => 'required'
        ], [
            'email.required' => '手机不能为空'
        ]);

        $ret=ComEmailCodeFacade::sendValidateCode($email,$isSend,$language,$footer);
        return $this->jsonResponse('ok', $ret);
    }


    public function upload(Request $request)
    {
        $upload = UploadFileFacade::upload($request, $request->input('field', 'file'));
        return $this->jsonResponse('ok', $upload);
    }
}
